package com.chat.model;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonProperty;

import java.time.Instant;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 聊天消息数据模型
 * 用于表示不同类型的WebSocket消息
 */
public class ChatMessage {
    
    /**
     * 消息类型：JOIN(加入), CHAT(聊天), LEAVE(离开), USER_LIST(用户列表), ERROR(错误)
     */
    private String type;
    
    /**
     * 用户名
     */
    private String username;
    
    /**
     * 消息内容
     */
    private String message;
    
    /**
     * 时间戳
     */
    private Instant timestamp;
    
    /**
     * 在线用户列表（用于USER_LIST类型消息）
     */
    private List<String> users;
    
    /**
     * 默认构造函数
     */
    public ChatMessage() {
        this.timestamp = Instant.now();
    }
    
    /**
     * 构造函数
     */
    public ChatMessage(String type, String username, String message) {
        this.type = type;
        this.username = username;
        this.message = message;
        this.timestamp = Instant.now();
    }
    
    /**
     * 创建用户加入消息
     */
    public static ChatMessage createJoinMessage(String username) {
        return new ChatMessage("JOIN", username, username + " 加入了聊天室");
    }
    
    /**
     * 创建用户离开消息
     */
    public static ChatMessage createLeaveMessage(String username) {
        return new ChatMessage("LEAVE", username, username + " 离开了聊天室");
    }
    
    /**
     * 创建聊天消息
     */
    public static ChatMessage createChatMessage(String username, String message) {
        return new ChatMessage("CHAT", username, message);
    }
    
    /**
     * 创建用户列表消息
     */
    public static ChatMessage createUserListMessage(List<String> users) {
        ChatMessage message = new ChatMessage();
        message.setType("USER_LIST");
        message.setUsers(users);
        return message;
    }
    
    // Getter和Setter方法
    public String getType() {
        return type;
    }
    
    public void setType(String type) {
        this.type = type;
    }
    
    public String getUsername() {
        return username;
    }
    
    public void setUsername(String username) {
        this.username = username;
    }
    
    public String getMessage() {
        return message;
    }
    
    public void setMessage(String message) {
        this.message = message;
    }
    
    public Instant getTimestamp() {
        return timestamp;
    }
    
    public void setTimestamp(Instant timestamp) {
        this.timestamp = timestamp;
    }
    
    public List<String> getUsers() {
        return users;
    }
    
    public void setUsers(List<String> users) {
        this.users = users;
    }
    
    @Override
    public String toString() {
        return "ChatMessage{" +
                "type='" + type + '\'' +
                ", username='" + username + '\'' +
                ", message='" + message + '\'' +
                ", timestamp=" + timestamp +
                ", users=" + users +
                '}';
    }
}